package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.StrUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.dto.FrontShipperUpdateGoodsStatusDto;
import com.weixing.mall.provider.model.enums.OrderStateEnum;
import com.weixing.mall.provider.model.enums.ProductStatusEnum;
import com.weixing.mall.provider.model.query.FrontShipperOrderSearchQuery;
import com.weixing.mall.provider.model.query.OrderVoQuery;
import com.weixing.mall.provider.model.query.ScanCodeGoodsDetilQuery;
import com.weixing.mall.provider.model.query.ShipperOrderQuery;
import com.weixing.mall.provider.model.vo.OrderShowVo;
import com.weixing.mall.provider.model.vo.ScanCodeGoodsDetilVo;
import com.weixing.mall.provider.service.IOrderService;
import com.weixing.mall.provider.service.impl.FrontShipperOrderServerImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author wx
 * @Date 2019/11/14 10:58
 * @Version 1.0
 */
@RestController
@RequestMapping("front/shipperOrder")
@Api(value = "前端:发货员订单管理API",tags = "前端:发货员订单管理")
public class FrontShipperOrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private FrontShipperOrderServerImpl frontShipperOrderServer;

    @ApiOperation(value = "订单列表查询")
    @GetMapping("")
    public Result unshippedOrder(@SysInfo SysAuth sysAuth, ShipperOrderQuery shipperOrderQuery){
        return ResultUtil.success(orderService.unshippedOrder(shipperOrderQuery,initMpPage(),sysAuth.getUid()));
    }

    @ApiOperation(value = "已发货列表查询")
    @GetMapping("deliver")
    public Result deliverOrderList(@SysInfo SysAuth sysAuth, ShipperOrderQuery shipperOrderQuery){
        return ResultUtil.success(orderService.deliverOrderList(shipperOrderQuery,initMpPage(),sysAuth.getUid()));
    }

    @ApiOperation(value = "发货扫描")
    @GetMapping("scanCode/{code}")
    public Result unshippedOrder(@SysInfo SysAuth sysAuth, @PathVariable String code){
        String productSn = "";
        String orderSn = "";
        if(StrUtil.isNotEmpty(code)){
            String [] ss=code.split("_");
            productSn=ss[1];
            orderSn=ss[2];
        }
        ShipperOrderQuery shipperOrderQuery = new ShipperOrderQuery();
        shipperOrderQuery.setOrderStatus(OrderStateEnum.WAIT_SHIP);
        shipperOrderQuery.setProductSn(productSn);
        shipperOrderQuery.setOrderSn(orderSn);
        return ResultUtil.success(orderService.unshippedOrder(shipperOrderQuery,initMpPage(),sysAuth.getUid()));
    }

    @ApiOperation(value = "商品状态修改")
    @PostMapping("updateGoodsStatus")
    public Result updateGoodsStatus(@RequestBody FrontShipperUpdateGoodsStatusDto frontShipperUpdateGoodsStatusDto){
        orderService.updateGoodsStatus(frontShipperUpdateGoodsStatusDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "发货")
    @GetMapping("confirmShipments")
 @NoRepeatSubmit
    /*@NoNeedAccess*/
    public Result confirmShipments(@SysInfo SysAuth auth,Long orderId,String depotSn,String expressSn ){
       /* auth =new SysAuth();
        auth.setUid(384L);
        auth.setUr("appyg,appyg");*/
        orderService.confirmShipments(orderId,auth,depotSn,expressSn);
        return ResultUtil.success();
    }

    @ApiOperation(value = "加载app订单详情")
    @GetMapping("detail")
    public Result getOrder(FrontShipperOrderSearchQuery entity) {
        Map<String,Object> result = frontShipperOrderServer.selectShipperOrder(entity);
//        System.out.println(result.toString());
        return ResultUtil.success(result);
    }

    @ApiOperation(value = "扫码加载订单详情")
    @GetMapping(value = "scanCodeDetails")
    public Result scanCodeDetails(ScanCodeGoodsDetilQuery scanCodeGoodsDetilQuery){
        ScanCodeGoodsDetilVo scanCodeGoodsDetilVo = orderService.selectScanCodeGoodsDetails(scanCodeGoodsDetilQuery);
        if(scanCodeGoodsDetilVo == null){//未绑定订单 查询商品信息
            return ResultUtil.success(orderService.selectScanCodeGoodsDetails(scanCodeGoodsDetilQuery.getQrCodeValue()));
        }
        return ResultUtil.success(scanCodeGoodsDetilVo);
    }

    @ApiOperation(value = "重置订单货品状态")
    @PostMapping("resetOrder/{orderId}")
    @NoRepeatSubmit
    public Result resetOrderStatus(@SysInfo SysAuth auth,@PathVariable(value = "orderId") Long orderId){
        orderService.resetOrderStatus(orderId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "换码")
    @PostMapping("changeCode/{orderId}/{productId}")
    @NoRepeatSubmit
    public Result changsCode(@SysInfo SysAuth auth,@PathVariable(value = "orderId") Long orderId,@PathVariable(value = "productId") Long productId){
        orderService.changsCode(orderId,productId,true);
        return ResultUtil.success();
    }

    /**
     * 发货员扫描订单核查，检查该订单是否属于该发货员
     * @param sysAuth
     * @param orderSn 订单编号
     * @return
     */
    @ApiOperation(value = "订单核查")
    @GetMapping("/checkOrder/{orderSn}")
    public Result checkOrder(@SysInfo SysAuth sysAuth, @PathVariable String orderSn){
       Order order= orderService.findByOrderSn(orderSn);
       if(null==order.getShipperId()){
           return ResultUtil.error("发货员为空，请检查订单");
       }
       if(order.getShipperId().longValue()!=sysAuth.getUid().longValue()){
           return ResultUtil.error("此订单属于"+ order.getShipperName()+"发货员");
       }
        return ResultUtil.success();
    }

}
